package fm.dongman.animefm.view.widget;

import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.StyleRes;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import fm.dongman.animefm.R;

/**
 * Created by alex on 2017/10/18.
 *
 * @description 带有一个EditText的dialog
 */

public class EditTextDialog extends Dialog {

    private EditTextDialog(@NonNull Context context, @StyleRes int themeResId) {
        super(context, themeResId);
    }

    public static class Builder {
        private String mTitle;
        private Context mContext;
        private OnButtonClickListener mListener;

        public Builder(Context context) {
            mContext = context;
        }

        public Builder setTitle(String title) {
            mTitle = title;
            return this;
        }

        public Builder setOnButtonClickListenr(OnButtonClickListener listener) {
            mListener = listener;
            return this;
        }

        public EditTextDialog build() {
            final EditTextDialog dialog = new EditTextDialog(mContext, R.style.CustomDialog);
            View layoutView = View.inflate(mContext, R.layout.dialog_edit_text, null);
            dialog.setContentView(layoutView);
            TextView title = (TextView) layoutView.findViewById(R.id.mTvEditDialogTitle);
            if (mTitle != null) {
                title.setText(mTitle);
            }
            final EditText input = (EditText) layoutView.findViewById(R.id.mTvEditDialogEditText);
            Button cancel = (Button) layoutView.findViewById(R.id.mBtnEditDialogCancel);
            Button confirm = (Button) layoutView.findViewById(R.id.mBtnEditDialogConfirm);

            cancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mListener != null)
                        mListener.onCanceled(dialog);
                }
            });

            confirm.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mListener != null)
                        mListener.onCommit(dialog, input.getText().toString());
                }
            });

            return dialog;
        }

        public interface OnButtonClickListener {
            void onCommit(Dialog dialog, String inputContent);

            void onCanceled(Dialog dialog);
        }
    }
}
